package class02;

/**
 * @author Gong Cheng
 * @date 2021/2/25 3:08 下午
 **/
public class Code02_eleteGIvenValue {
    public static class Node{
        public int value;
        public Node next;

        public Node(int data){
            this.value = data;
        }
    }

    // 删除链表中的固定值
    public static Node removeValue(Node head, int num){
        // 删头部
        while (head != null){
            if (head.value != num){
                break;
            }
            head = head.next;
        }
        // head 来到第一个不需要删的位置
        Node pre = head;
        Node cur = head;
        while (cur != null){
            if(cur.value == num){
                pre.next = cur.next;
            }else {
                pre = cur;
            }
            cur = cur.next;
        }
        return head;
    }
}
